package com.zxm.sunshineaiary.activity;

import java.util.ArrayList;
import java.util.Calendar;

import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zxm.sunshineaiary.activity.base.MybaseActivity;
import com.zxm.sunshineaiary.info.DateInfo;
import com.zxm.sunshineaiary.sql.service.SqlService;
import com.zxm.sunshineaiary.util.ColorUtil;
import com.zxm.sunshineaiary.util.StorageFile;
import com.zxm.sunshineaiary.view.mianactivity.PageControlView;
import com.zxm.sunshineaiary.view.mianactivity.ScrollHorizontalViewGroup;

/**
 * 用户主菜单界面
 * 
 * @author joker
 * 
 */
public class MainActivity extends MybaseActivity implements OnClickListener {

	private static final String TAG = "MainActivity";
	private final MainActivity instance = this;

	private PageControlView mPageControlView = null;
	private StorageFile msStorageFile = StorageFile.getInsance();
	private Calendar mCalendar = Calendar.getInstance();
	private ArrayList<String> mCursorList = null;
	private SqlService mSqlService = null;;
	private ScrollHorizontalViewGroup mViewGroup = null;
	private LinearLayout mDiaryLayout = null;
	private TextView mTitleText = null;
	// private TextView mCteateText = null;
	private TextView mWelcomeText = null;
	private TextView mWelcomeDay = null;
	private TextView mWelcomeMoth = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mSqlService = SqlService.getInstance(this);
		mViewGroup = (ScrollHorizontalViewGroup) findViewById(R.id.myViewGroup);
		findPageControlView();
		addViewOnViewGroup(mViewGroup);
		isCurrentYear();
		findView();
		setTextColor();
		createDiaryClick(mCursorList);
		setClickListener();
	}

	private void findPageControlView() {
		mPageControlView = (PageControlView) this
				.findViewById(R.id.pageControlView);
		mPageControlView.setCount(2);
	}

	private void addViewOnViewGroup(ScrollHorizontalViewGroup viewGroup) {
		mViewGroup.setScrollToScreenCallback(mPageControlView);
		viewGroup.addView(View.inflate(this, R.layout.welcome, null));
		viewGroup.addView(View.inflate(this, R.layout.menu, null));
	}

	private void findView() {
		mWelcomeDay = (TextView) this.findViewById(R.id.Text_welcome_day);
		mWelcomeMoth = (TextView) this.findViewById(R.id.Text_welcome_moth);
		mDiaryLayout = (LinearLayout) this.findViewById(R.id.menu_diary_layout);
		// mCteateText = (TextView)
		// this.findViewById(R.id.menu_text_cteate_diary);
		mTitleText = (TextView) this.findViewById(R.id.menu_text_diarydate);
	}

	private void setTextColor() {
		ColorUtil.setTextColor(mWelcomeDay);
		ColorUtil.setTextColor(mWelcomeMoth);
		ColorUtil.setTextColor(mTitleText);
	}

	private void setClickListener() {
		// mCteateText.setOnClickListener(this);
	}

	private void isCurrentYear() {
		mSqlService.insertSettingYear(mCalendar.get(Calendar.YEAR) + "");
		mCursorList = mSqlService.getSettingYearAll();
	}

	@Override
	public void onClick(View v) {
		final int i = 0;
		switch (v.getId()) {

		}
	}

	/**
	 * 增加新的年按钮
	 */
	private void createDiaryClick(ArrayList<String> mCursorList) {
		for (int i = 0; i < mCursorList.size(); i++) {
			TextView textView = new TextView(this);
			textView.setText(mCursorList.get(i));
			ColorUtil.setTextColor(textView);
			textView.setTextSize(90);
			textView.setTypeface(Typeface.DEFAULT);
			mDiaryLayout.addView(textView, 0);
			// mDiaryLayout.setBackgroundColor(Color.BLACK);
			setYearButtonClick(textView);
		}
	}

	private void setYearButtonClick(View v) {
		v.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				TextView tx = (TextView) v;
				DateInfo.getInstance().year = tx.getText().toString();
				Log.v(TAG, "year===" + DateInfo.getInstance().year);

				startCreateTextAnimation();
				startTitleAnimation();
			}
		});
	}

	/**
	 * 开始文字消失动画
	 */
	private void startCreateTextAnimation() {
		Animation mAnimation = AnimationUtils.loadAnimation(this,
				R.anim.create_diary_translate);
		startAnimation(mDiaryLayout, mAnimation);
	}

	/**
	 * 开始Title文字消失动画
	 */
	private void startTitleAnimation() {
		Animation mAnimation = AnimationUtils.loadAnimation(this,
				R.anim.diarydate_translate);
		mAnimation.setStartOffset(100);// 延迟0.1秒
		startAnimation(mTitleText, mAnimation);
		mAnimation.setAnimationListener(new myAnmationLisenner());
	}

	private void startAnimation(View view, Animation myAnimation) {
		myAnimation.setFillAfter(true);
		view.startAnimation(myAnimation);
	}

	/**
	 * 动画监听
	 * 
	 * @author Administrator
	 * 
	 */
	class myAnmationLisenner implements AnimationListener {

		@Override
		public void onAnimationEnd(Animation animation) {
			startNextActivity(DateMenuActivity.class);
		}

		@Override
		public void onAnimationRepeat(Animation animation) {

		}

		@Override
		public void onAnimationStart(Animation animation) {

		}
	}
}
